package br.uece.laconi.tac.strategy;

import se.sics.tac.aw.Bid;
import se.sics.tac.aw.Quote;
import se.sics.tac.aw.TACAgent;
import br.uece.laconi.tac.Agent;

public class HotelStrategy extends AgentStrategy {

	protected HotelKnowledge history = null;
	
	public HotelStrategy(Agent agent) {
		super(agent);
		history = new HotelKnowledge();
	}

	@Override
	public void init() {
		// Trecho de DummyAgent.calculateAllocation()
		for (int i = 0; i < 8; i++) {
			int type;
			int inFlight = sensor.getClientPreference(i, TACAgent.ARRIVAL);
			int outFlight = sensor.getClientPreference(i, TACAgent.DEPARTURE);
			int hotel = sensor.getClientPreference(i, TACAgent.HOTEL_VALUE);
			
//			if (hotel > 70) {
			if (hotel >= 0) {
				type = TACAgent.TYPE_GOOD_HOTEL;
			} else {
				type = TACAgent.TYPE_CHEAP_HOTEL;
			}
			
			for (int d = inFlight; d < outFlight; d++) {
				int auction = TACAgent.getAuctionFor(TACAgent.CAT_HOTEL, type, d);
				effector.log.finer("Adding hotel for day: " + d + " on " + auction);
				effector.setAllocation(auction, sensor.getAllocation(auction) + 1);
			}
		}

		//Trecho de DummyAgent.sendBids()
		for (int i = 0, n = TACAgent.getAuctionNo(); i < n; i++) {
			int alloc = sensor.getAllocation(i) - sensor.getOwn(i);
			if (TACAgent.getAuctionCategory(i) == TACAgent.CAT_HOTEL && alloc > 0) {
				Bid bid = new Bid(i);
				bid.addBidPoint(alloc, 200);
				history.update(bid);
				effector.submitBid(bid);
			}
		}		
	}

	@Override
	public void execute() {
		Quote quote = history.getLastQuote();
		int auction = quote.getAuction();
		int alloc = sensor.getAllocation(auction);
		if (alloc > 0 && quote.hasHQW(sensor.getBid(auction)) &&
				quote.getHQW() < alloc) {
			Bid bid = new Bid(auction);
			bid.addBidPoint(alloc, quote.getAskPrice() + 50);
			history.update(bid);
			effector.submitBid(bid);
		}
	}

	public void update(Object object) {
		if (object instanceof Quote) {
			Quote quote = (Quote) object;
			history.update(quote);
		}
		execute();
	}
}
